package com.survey.wxapp.controller;

import com.survey.common.annotation.Anonymous;
import com.survey.common.config.RuoYiConfig;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.utils.file.FileUploadUtils;
import com.survey.common.utils.file.FileUtils;
import com.survey.framework.config.ServerConfig;
import com.survey.system.service.ISysConfigService;
import com.survey.web.controller.common.CommonController;
import com.survey.wxapp.common.wxconfig.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Paths;

/**
 * @Author tyro
 * @Description
 * @Data 2/5/2024 下午 10:49
 * @Version
 */
@Anonymous
@RestController
@RequestMapping("/wx")
public class WxCommonController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private RuoYiConfig ruoyiConfig;

    @Autowired
    private ISysConfigService configService;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, file);
            // 移除 '/profile/upload/' 前缀，假设 fileName 中包含 '/profile/upload/'
            String filePrefix = "/profile/upload/";
            String fileNameToCompress = "";
            if (fileName.startsWith(filePrefix)) {
                fileNameToCompress = fileName.substring(filePrefix.length());
            }
            // 将文件名与存储路径拼接成完整的本地文件路径
            String originalFilePath = Paths.get(filePath, fileNameToCompress).toString(); // 本地物理路径
            System.out.println("originalFilePath: " + originalFilePath);
            // 定义缩略图文件的存储路径
            String thumbnailFileName = "compress_" + FileUtils.getName(fileName); // 提取文件名
            String thumbnailFilePath = Paths.get(filePath, thumbnailFileName).toString(); // 缩略图路径

            // 使用 ImageUtil 生成缩略图
            boolean thumbnailSuccess = ImageUtil.storeThumbnailWithImage(originalFilePath, thumbnailFilePath);

            // 如果缩略图生成失败，返回错误信息
            if (!thumbnailSuccess) {
                return AjaxResult.error("生成缩略图失败！");
            }
            // 返回结果
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName); // 文件的相对路径
            ajax.put("compressFileName", "/profile/upload/"+thumbnailFileName); // 缩略图文件名
            return ajax;
        } catch (Exception e) {
            // 记录异常信息
            log.error("文件上传失败: {}", e.getMessage());
            return AjaxResult.error("文件上传失败: " + e.getMessage());
        }
    }
}
